package com.example.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.crm.entity.Opportunity;
import com.example.crm.service.OpportunityService;
import com.example.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 销售机会API控制器
 */
@RestController
@RequestMapping("/api/opportunity")
public class OpportunityController {

    @Autowired
    private OpportunityService opportunityService;

    /**
     * 分页查询销售机会列表
     */
    @GetMapping("/list")
    public Result<IPage<Opportunity>> list(@RequestParam(required = false) Map<String, Object> params) {
        if (params == null) {
            params = new HashMap<>();
        }
        IPage<Opportunity> page = opportunityService.listOpportunitiesByPage(params);
        return Result.success(page);
    }

    /**
     * 根据客户ID获取销售机会列表
     */
    @GetMapping("/customer/{customerId}")
    public Result<List<Opportunity>> getByCustomerId(@PathVariable Long customerId) {
        List<Opportunity> opportunities = opportunityService.getOpportunitiesByCustomerId(customerId);
        return Result.success(opportunities);
    }

    /**
     * 获取销售机会详情
     */
    @GetMapping("/{id}")
    public Result<Opportunity> getById(@PathVariable Long id) {
        Opportunity opportunity = opportunityService.getOpportunityDetail(id);
        return Result.success(opportunity);
    }

    /**
     * 新增销售机会
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody Opportunity opportunity) {
        opportunity.setCreateTime(LocalDateTime.now());
        opportunity.setUpdateTime(LocalDateTime.now());
        opportunity.setDeleted(0);
        return Result.success(opportunityService.addOpportunity(opportunity));
    }

    /**
     * 更新销售机会
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody Opportunity opportunity) {
        opportunity.setUpdateTime(LocalDateTime.now());
        return Result.success(opportunityService.updateOpportunity(opportunity));
    }

    /**
     * 更新销售机会阶段
     */
    @PostMapping("/stage/{id}")
    public Result<Boolean> updateStage(@PathVariable Long id, @RequestParam Integer stage, @RequestParam(required = false) String reason) {
        return Result.success(opportunityService.updateOpportunityStage(id, stage, reason));
    }

    /**
     * 删除销售机会
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(opportunityService.removeById(id));
    }

    /**
     * 获取销售漏斗数据
     */
    @GetMapping("/funnel")
    public Result<Map<String, Object>> getSalesFunnel() {
        return Result.success(opportunityService.getSalesFunnelData());
    }

    /**
     * 获取销售统计数据
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics() {
        return Result.success(opportunityService.getSalesStatistics());
    }
} 